#coding=utf-8
import hashlib
import logging
import time
import collections
import django
import random
import json
from django.contrib import messages
from django.contrib.auth import authenticate, logout
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.models import User
from django.contrib import auth
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.db import transaction
from django.db.utils import IntegrityError
from django.http import HttpResponse
from django.shortcuts import (get_object_or_404, redirect, render,
                              render_to_response)
from django.template import RequestContext
from django.views.generic import View
from django.views.generic.detail import DetailView
from django.views.generic.edit import (CreateView, DeleteView, FormView,
                                       UpdateView)
from django.views.generic.list import ListView
from main import models
import datetime

logger = logging.getLogger('main')

def home(request):
    return render(request, 'login/login.html')
def index(request):
    user = request.user
    if user and user.is_active:
        return render(request, 'main/index.html')
    else:
        return render(request, 'login/login.html')
def login(request):
    value = request.POST.get('KEYDATA','')
    loginname = request.POST.get('loginname','')
    password = request.POST.get('password','')
    try:
        
        user = auth.authenticate(username=loginname,password=password)
        name_dict = {'result':'usererror','twz': 'Love python and Django', 'zqxt': 'I am teaching Django'}
        if user and user.is_active:
            auth.login(request, user)
            name_dict = {'result':'success','twz': 'Love python and Django', 'zqxt': 'I am teaching Django'}
            logger.info('login -message keydata:%s,username:%s',value,user.username)
    #return HttpResponse("hello")
    except Exception as e:
        logger.info("%s",e)
    return HttpResponse(json.dumps(name_dict), content_type='application/json')  
    #return render(request, 'admin/index.html')
def logout(request):
    auth.logout(request)
    return render(request, 'login/login.html')
 